package com.example.aspect;

import com.example.util.CryptUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @Auther: tong
 * @Date: 2023/01/19/14:53
 * @Description:
 */
@Component
@Aspect
public class MyAOP {
    @Pointcut("execution(* com.example.controller..*.fortuneTelling(..))")
    public void pointCut() {
    }

    @Pointcut("@annotation(com.example.aspect.EnCodeAndDeCode)")
    public void annotationPointCut() {
    }

    @Around("annotationPointCut()")
    public Object around(ProceedingJoinPoint point) {
        Object proceed = null;
        Object[] args = point.getArgs();
        System.out.println("明文参数是:" + Arrays.toString(args));
        for (int i = 0; i < args.length; i++) {
            args[i] = CryptUtil.AESdecode((String) args[i]);
        }
        System.out.println("密文参数是:" + Arrays.toString(args));
        try {
            proceed = point.proceed(args);
            System.out.println("明文返回值是:" + proceed);
            System.out.println("密文返回值是:" + CryptUtil.AESencode((String) proceed));
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return CryptUtil.AESencode((String) proceed);
    }
}
